// Grid.js - 部门表格配置
const deptGridOptions = {
  columns: [
    { name: '编号', id: 'id', width: '8%' },
    { name: '父部门', id: 'parentId', width: '12%' },
    { name: '名称', id: 'name', sort: true },
    { name: '类型', id: 'type', sort: true },
    { name: '负责人', id: 'principal', sort: true },
    { name: '电话', id: 'phone', sort: true },
    { name: '备注', id: 'remark', sort: true },
    {
      name: '操作',
      id: 'actions',
      width: '20%',
      formatter: (_, row) => gridjs.html(`
        <button onclick="openEditModal('${row.cells[0].data}')" class="btn btn-sm btn-outline-primary m-1">
          <i class="fas fa-edit"></i> 编辑
        </button>
        <button onclick="toggleDept('${row.cells[0].data}')" class="btn btn-sm btn-outline-warning m-1">
          <i class="fas fa-exchange"></i> 切换
        </button>
      `)
    }
  ],
  server: {
    url: "/api/rest/dept",
    then: data => {
      window.deptData = data.content;
      return data.content.map(
        dept => [
          dept.id,
          dept.parentId,
          dept.name,
          dept.type,
          dept.principal,
          dept.phone,
          dept.remark
        ]
      );
    },
    total: data => data.totalElements,
    onError: res => res.text().then(txt => {
      console.error('部门数据获取错误:', txt);
      showToast('获取部门数据失败：' + txt, false);
    })
  },
  pagination: {
    limit: 10,
    server: {
      url: (prev, page, limit) => addQueryParam(prev, `size=${limit}&page=${page}`)
    }
  },
  search: false,
  sort: true,
  language: {
    pagination: {
      previous: '上一页',
      next: '下一页',
      showing: '显示第',
      of: '条，共',
      to: '至',
      results: '条记录'
    }
  }
};